#include <logbase.h>
#include <errno.h>
#include <stdarg.h>
#include <fileOpt.h>

#define checkFileName "/tmp/tmp.txt"
#define checkBufSize 10
#define checkPackSize 20
int main()
{
    srand(time(NULL));
    int tmpidbuf[checkBufSize];
    for(int i = 0; i < checkBufSize; i++)
    {
        tmpidbuf[i] = i;
    }
    // 打乱位置
    for(int i = 0; i < checkBufSize; i++)
    {
        int tmpid = -1;
        while(!(tmpid > 0 && tmpid != i))
        {
            tmpid = rand() % checkBufSize;
        }
        SWAP_BASE_VAL(tmpidbuf[i], tmpidbuf[tmpid]);
    }
    for(int i = 0; i < checkBufSize; i++)
    {
        logdebugc("%d", tmpidbuf[i]);
    }

    char tmpstrbuf[checkBufSize][checkPackSize];
    for(int i = 0; i < checkBufSize; i++)
    {
        memset(tmpstrbuf[i], '~', sizeof(tmpstrbuf[i]));
        int tmplen = snprintf(tmpstrbuf[i], sizeof(tmpstrbuf[i]), "%09d", tmpidbuf[i]);
        if(tmplen < sizeof(tmpstrbuf[i]))
        {
            tmpstrbuf[i][tmplen] = '~';
        }
        tmpstrbuf[i][sizeof(tmpstrbuf[i]) - 1] = '\n';
    }
    for(int i = 0; i < checkBufSize; i++)
    {
        logdebugc("%.*s", (int)sizeof(tmpstrbuf[i]) - 1, tmpstrbuf[i]);
        // logdebugc("%s", tmpstrbuf[i]);
    }

    FILE *tmpFd = fopen(checkFileName, "w+");
    if(!tmpFd)
    {
        logerrorc("open error %s", checkFileName);
        return -1;
    }
    for(int i = 0; i < /*2*/checkBufSize; i++)
    {
        fseek(tmpFd, sizeof(tmpstrbuf[i]) * tmpidbuf[i], SEEK_SET);
        fwrite(tmpstrbuf[i], sizeof(tmpstrbuf[i]), 1, tmpFd);
        // logdebugc("%s", tmpstrbuf[i]);
    }

    fclose(tmpFd);
    return 0;
}

